import os
import random
import predict_dist_3_verify as predict
import verify_util as vu
import pandas as pd
import matplotlib.pyplot as plt


def generate_unique_random_numbers(start, end, count):
    # 确保 count 不超过范围的长度
    count = min(count, end - start + 1)

    # 生成不重复的随机数列表
    random_numbers = random.sample(range(start, end + 1), count)

    return random_numbers


def get_random_image_paths(folder_path, num_images):
    # 获取文件夹下所有图片文件的路径
    image_files = [f for f in os.listdir(folder_path) if f.lower().
        endswith(('.png', '.jpg', '.jpeg', '.gif', '.bmp'))]

    # 确保要取出的数量不超过文件夹中的图片数量
    num_images = min(num_images, len(image_files))

    # 随机选择指定数量的图片路径
    random_image_paths = random.sample(image_files, num_images)

    # 构建完整的文件路径
    full_paths = [os.path.join(folder_path, image_path) for image_path in random_image_paths]

    return full_paths


def main(filename_path, filename, s_dir_path, c_dir_path, d_dir_path):
    num = generate_unique_random_numbers(1, 99, 2)
    left = min(num[0], num[1])
    right = max(num[0], num[1])

    s_data_count = left
    c_data_count = right - left
    d_data_count = 100 - s_data_count - c_data_count

    s_data_paths = get_random_image_paths(s_dir_path, s_data_count)
    c_data_paths = get_random_image_paths(c_dir_path, c_data_count)
    d_data_paths = get_random_image_paths(d_dir_path, d_data_count)

    # img_paths_list = []
    # img_paths_list.extend(s_data_paths)
    # img_paths_list.extend(c_data_paths)
    # img_paths_list.extend(d_data_paths)

    predict.get_data_from_path(filename_path, s_data_paths, c_data_paths, d_data_paths, filename)


def verify_data():
    s_dir_path = r"../data/four_data/source"
    c_dir_path = r"../data/four_data/Dclose2"
    d_dir_path = r"../data/four_data/Ddistance"
    for i in range(100):
        main(r"tmp", f"verify_{i}.xlsx", s_dir_path, c_dir_path, d_dir_path)
    results = []
    ans = []
    for i in range(100):
        print(f"./tmp/verify_{i}.xlsx.xlsx")
        try:
            itm = vu.verify(f"./tmp/verify_{i}.xlsx.xlsx")
        except:
            continue
        results.append(itm)
    for i in results:
        i[0].append(i[0][0] + i[0][1])
        i[0].append(i[0][0] / (i[0][0] + i[0][1]))
        i[1].append(i[1][0] + i[1][1])
        i[1].append(i[1][1] / (i[1][0] + i[1][1]))
        i[2].append(i[2][0] + i[2][1])
        i[2].append(i[2][1] / (i[2][0] + i[2][1]))
        i.append((i[0][0] + i[0][1] + i[1][0] + i[1][1] + i[2][0] + i[2][1]))
    for data in results:
        flattened_data = \
            [item for sublist in data for item in
             (sublist if isinstance(sublist, list)
              else [sublist])]
        ans.append(flattened_data)
    print(ans)
    labels = ["是", "否", "Sdata 总数", "Sdata 召回率", "是", "否", "Cdata 总数",
              "Cdata 精确度", "是", "否", "Ddata 总数", "Ddata 精确度", "总个数"]
    df = pd.DataFrame(ans, columns=labels)
    # vu.verify(f"./verify_{21}.xlsx.xlsx")
    # 保存为 Excel 文件
    df.to_excel('output_verify.xlsx', index=False)


def verify_data_2():
    s_dir_path = r"../data/four_data/source"
    c_dir_path = r"../data/four_data/Dclose2_2"
    d_dir_path = r"../data/four_data/Ddistance_2"
    for i in range(120):
        main(r"tmp_2", f"verify_{i}.xlsx", s_dir_path, c_dir_path, d_dir_path)
    results = []
    ans = []
    for i in range(120):
        print(f"./tmp_2/verify_{i}.xlsx.xlsx")
        # try:
        #     itm = vu.verify(f"./tmp_2/verify_{i}.xlsx.xlsx")
        # except:
        #     continue
        itm = vu.verify_2(f"./tmp_2/verify_{i}.xlsx.xlsx")
        results.append(itm)
    for i in results:
        i[0].append(i[0][0] + i[0][1])
        i[0].append(i[0][0] / (i[0][0] + i[0][1]))
        i[1].append(i[1][0] + i[1][1])
        i[1].append(i[1][1] / (i[1][0] + i[1][1]))
        i[2].append(i[2][0] + i[2][1])
        i[2].append(i[2][1] / (i[2][0] + i[2][1]))
        i.append((i[0][0] + i[0][1] + i[1][0] + i[1][1] + i[2][0] + i[2][1]))
    for data in results:
        flattened_data = \
            [item for sublist in data for item in
             (sublist if isinstance(sublist, list)
              else [sublist])]
        ans.append(flattened_data)
    print(ans)
    labels = ["是", "否", "Sdata 总数", "Sdata 召回率", "是", "否", "Cdata 总数",
              "Cdata 精确度", "是", "否", "Ddata 总数", "Ddata 精确度", "总个数"]
    df = pd.DataFrame(ans, columns=labels)
    # vu.verify(f"./verify_{21}.xlsx.xlsx")
    # 保存为 Excel 文件
    df.to_excel('output_verify_2.xlsx', index=False)


if __name__ == '__main__':
    # verify_data()
    verify_data_2()
